/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.view.analysis;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import systole.domain.analysis.Analysis;
import systole.domain.persons.Medic;
import systole.domain.persons.Patient;
import systole.exceptions.ExceptionDAO;
import systole.persistence.FacadeDB;
import systole.view.MainController;
import systole.view.abstractController.AbstractViewController;
import systole.view.crud.tableModels.AnalysisTableModel;
import systole.view.utils.ErrorDialog;

/**
 *
 * @author jmj
 */
public class ControllerAnalysisView extends AbstractViewController {

    private FacadeDB facadeDB;
    private List<Analysis> entityList;
    private AnalysisTableModel tableModel;
    private JDialogAnalysis analysisView;
    private JDialogResults dialogResults;
    private MainController mainController;
    private boolean changeFilter = false;

    /**
     * @param mainController
     * @throws ExceptionDAO
     */
    public ControllerAnalysisView(MainController mainController) throws ExceptionDAO {
        super();
        this.facadeDB = FacadeDB.getInstance();
        this.entityList = this.facadeDB.getAnalysisBroker().getAllAnalisys();
        this.tableModel = new AnalysisTableModel(this.entityList);
        this.mainController = mainController;
    }

    /**
     * @param parent
     */
    public void showForm(java.awt.Frame parent) {
        this.analysisView = new JDialogAnalysis(parent, this);

        this.analysisView.getjCmbPatients().removeAllItems();
        try {
            List<Patient> patients = this.facadeDB.getPatientBroker().getAllPatient();
            Iterator<Patient> itPatients = patients.iterator();
            while (itPatients.hasNext()) {
                this.analysisView.getjCmbPatients().addItem(itPatients.next());
            }

            this.analysisView.getjCmbMedics().removeAllItems();

            List<Medic> medics = this.facadeDB.getMedicBroker().getAllMedics();
            Iterator<Medic> itMedics = medics.iterator();
            while (itMedics.hasNext()) {
                this.analysisView.getjCmbMedics().addItem(itMedics.next());
            }
            this.analysisView.setLocationRelativeTo(parent);
            this.analysisView.getjXTableAnalysis().setModel(this.tableModel);
            this.setCloseListener(analysisView);
            this.analysisView.setVisible(true);
        } catch (ExceptionDAO e) {
            this.facadeDB.refreshSession();
            ErrorDialog.showError(this.analysisView, e.getMessage());
            this.analysisView.dispose();
        }
    }

    public void printAnalysis() {
    }

    /**
     * 
     */
    public void closeForm() {
        this.analysisView.setVisible(false);
        this.analysisView.dispose();
    }

    /**
     * 
     */
    public void open() {
        int selected = this.analysisView.getjXTableAnalysis().getSelectedRow();
        if (selected != -1) {
            Analysis analysis = this.entityList.get(selected);
            this.closeForm();
            this.mainController.openAnalysis(analysis);
        }
    }

    /**
     * 
     */
    public void showParams() {
        int selected = this.analysisView.getjXTableAnalysis().getSelectedRow();
        if (selected != -1) {
            Analysis analysis = this.entityList.get(selected);
            if (analysis.getAnalysisResult() != null) {
                this.dialogResults = new JDialogResults(null, analysis.getAnalysisResult(), this.analysisView);
                this.dialogResults.setVisible(true);
            } else {
                ErrorDialog.showError(this.analysisView, "El analisis no tiene resultados aún");
            }
        }
    }

    public void applyFilter() {
        if (this.changeFilter) {

            Medic medic = (this.analysisView.getjChkMedic().isSelected() ? (Medic) this.analysisView.getjCmbMedics().getSelectedItem() : null);
            Patient patient = (this.analysisView.getjChkPatient().isSelected() ? (Patient) this.analysisView.getjCmbPatients().getSelectedItem() : null);
            Date date = (this.analysisView.getjChkDate().isSelected() ? this.analysisView.getjXDateAnalysis().getDate() : null);
            try {
                this.entityList = this.facadeDB.getAnalysisBroker().getAnalysisByDateMedicaPatiente(date, patient, medic);
                this.tableModel.refreshList(this.entityList);
                this.analysisView.getjXTableAnalysis().setModel(this.tableModel);
                this.analysisView.getjXTableAnalysis().revalidate();
            } catch (ExceptionDAO e) {
                this.facadeDB.refreshSession();
                ErrorDialog.showError(this.analysisView, e.getMessage());
            }
            this.changeFilter = false;
        }
    }

    public void setFilterChanged() {
        this.changeFilter = true;
    }
}
